/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.InBoundMissionHistory;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.InBoundMissionHistoryRepository;
import me.zhengjie.service.InBoundMissionHistoryService;
import me.zhengjie.service.dto.InBoundMissionHistoryDto;
import me.zhengjie.service.dto.InBoundMissionHistoryQueryCriteria;
import me.zhengjie.service.mapper.InBoundMissionHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author zhangYY
* @date 2024-01-12
**/
@Service
@RequiredArgsConstructor
public class InBoundMissionHistoryServiceImpl implements InBoundMissionHistoryService {

    private final InBoundMissionHistoryRepository inBoundMissionHistoryRepository;
    private final InBoundMissionHistoryMapper inBoundMissionHistoryMapper;

    @Override
    public PageResult<InBoundMissionHistoryDto> queryAll(InBoundMissionHistoryQueryCriteria criteria, Pageable pageable){
        Page<InBoundMissionHistory> page = inBoundMissionHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(inBoundMissionHistoryMapper::toDto));
    }

    @Override
    public List<InBoundMissionHistoryDto> queryAll(InBoundMissionHistoryQueryCriteria criteria){
        return inBoundMissionHistoryMapper.toDto(inBoundMissionHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public InBoundMissionHistoryDto findById(Integer id) {
        InBoundMissionHistory inBoundMissionHistory = inBoundMissionHistoryRepository.findById(id).orElseGet(InBoundMissionHistory::new);
        ValidationUtil.isNull(inBoundMissionHistory.getId(),"InBoundMissionHistory","id",id);
        return inBoundMissionHistoryMapper.toDto(inBoundMissionHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(InBoundMissionHistory resources) {
        inBoundMissionHistoryRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InBoundMissionHistory resources) {
        InBoundMissionHistory inBoundMissionHistory = inBoundMissionHistoryRepository.findById(resources.getId()).orElseGet(InBoundMissionHistory::new);
        ValidationUtil.isNull( inBoundMissionHistory.getId(),"InBoundMissionHistory","id",resources.getId());
        inBoundMissionHistory.copy(resources);
        inBoundMissionHistoryRepository.save(inBoundMissionHistory);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            inBoundMissionHistoryRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InBoundMissionHistoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InBoundMissionHistoryDto inBoundMissionHistory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("载盘编码", inBoundMissionHistory.getPalletCode());
            map.put("任务状态", inBoundMissionHistory.getMissionStatus());
            map.put("起点", inBoundMissionHistory.getSource());
            map.put("终点", inBoundMissionHistory.getDestination());
            map.put("创建时间", inBoundMissionHistory.getCreateTime());
            map.put("开始时间", inBoundMissionHistory.getStartTime());
            map.put("结束时间", inBoundMissionHistory.getFinishTime());
            map.put("任务编码", inBoundMissionHistory.getMissionCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}